package com.augurit.gzcgwsp.sendNotice.dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import org.springside.modules.orm.Page;

import com.augurit.common.base.dao.BaseDao;
import com.augurit.gzcgwsp.sendNotice.entity.YwZhengwuInfo;

@Repository
public class YwZhengwuInfoDao extends BaseDao<YwZhengwuInfo, Long>{
	/**
	 * 保存webservice接口的信息
	 * @param ywZhengwuInfo
	 */
	public void save(YwZhengwuInfo ywZhengwuInfo){
		getSession().saveOrUpdate(ywZhengwuInfo);
	}
	
	/**
	 * 获得还没有发送的记录  政务中心接口
	 * @return
	 */
	public Page<YwZhengwuInfo> getYwZhengwuInfo(Page<YwZhengwuInfo> ywZhengwuInfoPage){
		String sql = "from YwZhengwuInfo zw where zw.isSended is null order by zw.produceDate asc";
        List<YwZhengwuInfo> entityList = this.find(sql);
        //return entityList;
        
        // 查询语句及参数
        StringBuffer hql = new StringBuffer();
        hql.append(sql);
        List values = new ArrayList();        
        // 执行分页查询操作
        this.findPage(ywZhengwuInfoPage, hql.toString(), values);
        
        return ywZhengwuInfoPage;
	}
	
	/**
	 * 获得还没有发送的特别程序结束的记录  政务中心接口
	 * @return
	 */
	public Page<YwZhengwuInfo> getYwZhengwuInfoForTbcxEnd(Page<YwZhengwuInfo> ywZhengwuInfoPage){
		Date date = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = sf.format(date);
		String sql = "from YwZhengwuInfo zw where (zw.isSended like '%延期结束时间%' or zw.isSended like '%公示结束时间%') and zw.yqEndDate <= to_date('"+dateStr+"','yyyy-MM-dd HH24:mi:ss') order by zw.produceDate asc";
        List<YwZhengwuInfo> entityList = this.find(sql);
        //return entityList;
        
        // 查询语句及参数
        StringBuffer hql = new StringBuffer();
        hql.append(sql);
        List values = new ArrayList();        
        // 执行分页查询操作
        this.findPage(ywZhengwuInfoPage, hql.toString(), values);
        
        return ywZhengwuInfoPage;
	}
	
	/**
	 * 当webservice调用成功，则把该记录的isSended字段的值改成1
	 * @param id
	 */
	public void changState(long id){
		Date date = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = sf.format(date);
		String hql = "update YwZhengwuInfo zw set zw.isSended='1',zw.sendTime=:sendTime where zw.id=:id";
		Query query = this.getSession().createQuery(hql);
		query.setString("sendTime", dateStr);
		query.setLong("id", id);
		query.executeUpdate();
	}
	
	/**
	 * 当webservice调用成功，则把该记录的isSended字段的值改成空
	 * @param id
	 */
	public void updateIsSended(long id){
		String hql = "update YwZhengwuInfo zw set zw.isSended='' where zw.id=:id";
		Query query = this.getSession().createQuery(hql);
		query.setLong("id", id);
		query.executeUpdate();
	}
	
	/**
	 * 办结前，根据受理号查询是否有还没有结束延期的记录
	 * @param serialNo
	 */
	public YwZhengwuInfo doYqEnd(String serialNo){
		String sql = "from YwZhengwuInfo zw where zw.isSended like '延期结束时间%' and zw.serialNo='"+serialNo+"' order by zw.produceDate asc";
        List<YwZhengwuInfo> entityList = this.find(sql);
        YwZhengwuInfo ywZhengwuInfo = null;
        if(entityList != null && entityList.size()>0){
        	ywZhengwuInfo = entityList.get(0);
        }
        return ywZhengwuInfo;
	}
}
